package com.wangsd.web.common.service;

import com.wangsd.common.utils.OssUtils;
import com.wangsd.web.common.entity.UploadFile;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class UploadFileServiceImpl implements UploadFileService {


    @Autowired
    OssUtils ossUtils;

    //文件上传地址
    @Value("${wangsd.upload_file}")
    private String uploadFile;

    //图片上传地址
    @Value("${wangsd.upload_img}")
    private String uploadImg;

    //p12证书上传地址
    @Value("${wangsd.upload_p12}")
    private String uploadP12;

    @Override
    public Object uploadImg(MultipartFile file) {
        String uid = UUID.randomUUID().toString();
        String fileName = uid + "." + FilenameUtils.getExtension(file.getOriginalFilename());
        String pathName = uploadImg + fileName;
        File dest = new File(pathName);
        try {
            File mulu = new File(FilenameUtils.getFullPath(pathName));
            if (!mulu.exists()) {
                mulu.mkdirs();
            }
            //Thumbnails.of(file.getInputStream()).scale(1f).outputQuality(0.25f).toFile(dest);
            Thumbnails.of(file.getInputStream()).scale(1f).toFile(dest);
        } catch (IOException e) {
            log.error("uploadImg",e);
        }

        String url = ossUtils.uploadFile("img", fileName, dest);

        UploadFile uploadFile = new UploadFile();
        uploadFile.setUid(uid);
        uploadFile.setName(fileName);
        uploadFile.setUrl(url);
        return uploadFile;
    }

    @Override
    public Object uploadFile(MultipartFile file) {
        String uid = UUID.randomUUID().toString();
        String fileName = uid + "." + FilenameUtils.getExtension(file.getOriginalFilename());
        String pathName = uploadFile + fileName;
        File dest = new File(pathName);
        try {
            File mulu = new File(FilenameUtils.getFullPath(pathName));
            if (!mulu.exists()) {
                mulu.mkdirs();
            }
            inputStreamToFile(file.getInputStream(), dest);
        } catch (Exception e) {
            log.error("uploadFile",e);
        }
        String url = ossUtils.uploadFile("file", fileName, dest);
        dest.delete();
        UploadFile uploadFile = new UploadFile();
        uploadFile.setName(fileName);
        uploadFile.setUrl(url);
        return uploadFile;
    }

    @Override
    public Object uploadCertificateFile(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String pathName = uploadP12 + fileName;
        File dest = new File(pathName);
        try {
            File mulu = new File(FilenameUtils.getFullPath(pathName));
            if (!mulu.exists()) {
                mulu.mkdirs();
            }
            inputStreamToFile(file.getInputStream(), dest);
            UploadFile uploadFile = new UploadFile();
            uploadFile.setUid(fileName);
            uploadFile.setName(fileName);
            uploadFile.setUrl(uploadP12 + fileName);
            return uploadFile;
        } catch (Exception e) {
            log.error("uploadCertificateFile",e);
        }
        return null;
    }

    @Override
    public void deleteCertificate(String name) {
        try {
            String pathName = uploadP12 + name;
            File mulu = new File(pathName);
            if (mulu.exists()) {
                mulu.delete();
            }
        } catch (Exception e) {
            log.error("deleteCertificate",e);
        }
    }

    @Override
    public void deleteImg(String name) {
        ossUtils.deleteFile("img/" + name);
    }

    @Override
    public void deleteFile(String name) {
        ossUtils.deleteFile("file/" + name);
    }

    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            log.error("inputStreamToFile",e);
        }
    }
}
